package shop.xmall.system.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import shop.xmall.framework.core.controller.BaseController;
import shop.xmall.framework.core.domain.ResponseResultDto;
import shop.xmall.framework.core.page.PageDto;
import shop.xmall.system.domain.SysUser;
import shop.xmall.system.service.ISysUserService;

import java.util.List;

/**
 * 用户信息Controller
 *
 * @author xmall
 * @date 2019-11-14
 */
@Controller
@RequestMapping("/system/user")
public class SysUserController extends BaseController {

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询用户信息信息列表
     */
    @PostMapping("/list")
    @ResponseBody
    public ResponseResultDto list(SysUser sysUser) {
        PageHelper.offsetPage(getPageNum() - 1, getPageSize());
        List<SysUser> list = sysUserService.selectSysUserList(sysUser);
        PageInfo pageInfo = new PageInfo(list);
        PageDto pageDto = new PageDto(list, pageInfo.getTotal(), getPageNum(), getPageSize());
        return ResponseResultDto.success(pageDto);
    }


    /**
     * 查询用户信息信息
     */
    @PostMapping("/getById")
    @ResponseBody
    public ResponseResultDto getById(Long userId) {
        return ResponseResultDto.success(sysUserService.selectSysUserById(userId));
    }


    /**
     * 新增保存用户信息信息
     */
    @PostMapping("/add")
    @ResponseBody
    public ResponseResultDto addSave(@Validated  SysUser sysUser) {
        //验证登陆名
        SysUser validateUser = new SysUser();
        validateUser.setLoginName(sysUser.getLoginName());
        if (sysUserService.isExist(validateUser) > 0) {
         return   ResponseResultDto.error("新增用户'" + sysUser.getLoginName() + "'失败，登录账号已存在");
        }
        //验证邮箱
        validateUser.setLoginName(null);
        validateUser.setEmail(sysUser.getEmail());
        if (sysUserService.isExist(validateUser) > 0) {
          return  ResponseResultDto.error("新增用户'" + sysUser.getLoginName() + "'失败，邮箱账号已存在");
        }
        //验证手机
        validateUser.setEmail(null);
        validateUser.setPhonenumber(sysUser.getPhonenumber());
        if (sysUserService.isExist(validateUser) > 0) {
            return  ResponseResultDto.error("新增用户'" + sysUser.getLoginName() + "'失败，手机号已存在");
        }

        return toAjax(sysUserService.insertSysUser(sysUser));
    }


    /**
     * 修改保存用户信息信息
     */
    @PostMapping("/edit")
    @ResponseBody
    public ResponseResultDto editSave(@Validated SysUser sysUser) {
        //验证登陆名
        SysUser validateUser = new SysUser();
        validateUser.setUserId(sysUser.getUserId());
        validateUser.setLoginName(sysUser.getLoginName());
        if (sysUserService.isExist(validateUser) > 0) {
            return   ResponseResultDto.error("修改用户'" + sysUser.getLoginName() + "'失败，登录账号已存在");
        }
        //验证邮箱
        validateUser.setLoginName(null);
        validateUser.setEmail(sysUser.getEmail());
        if (sysUserService.isExist(validateUser) > 0) {
            return  ResponseResultDto.error("修改用户'" + sysUser.getLoginName() + "'失败，邮箱账号已存在");
        }
        //验证手机
        validateUser.setEmail(null);
        validateUser.setPhonenumber(sysUser.getPhonenumber());
        if (sysUserService.isExist(validateUser) > 0) {
            return  ResponseResultDto.error("修改用户'" + sysUser.getLoginName() + "'失败，手机号已存在");
        }

        return toAjax(sysUserService.updateSysUser(sysUser));
    }

    /**
     * 删除用户信息信息
     */
    @PostMapping("/delete")
    @ResponseBody
    public ResponseResultDto remove(String ids) {
        return toAjax(sysUserService.deleteSysUserByIds(ids));
    }


    /**
     * 判断是否存在用户名、手机号、邮箱等
     */
    @PostMapping("/isExist")
    @ResponseBody
    public ResponseResultDto isExist(SysUser sysUser) {
        int count = sysUserService.isExist(sysUser);
        return toAjax(count);
    }
}
